package components;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.dom4j.Element;

public class LinguisticVariable {

	private ArrayList<LinguisticValue> values = new ArrayList<LinguisticValue>();
	private Color[] colors = new Color[5];
	private String name = "";

	public String getName() {
		return name;
	}

	public LinguisticVariable(Element e) {
		colors[0] = Color.BLACK;
		colors[1] = Color.RED;
		colors[2] = Color.BLUE;
		colors[3] = Color.GRAY;
		colors[4] = Color.GREEN;
		this.name = e.attributeValue("name");
		for (Object ele : e.elements("value")) {
			values.add(new LinguisticValue((Element) ele));
		}
	}

	public LinguisticVariable(String variableName) {
		this.name = variableName;
	}

	public ArrayList<String> getValueNames() {
		ArrayList<String> names = new ArrayList<String>();
		for (LinguisticValue value : values) {
			names.add(value.getName());
		}
		return names;
	}

	public void setValueNames(String valueName) {
		values.add(new LinguisticValue(valueName));

	}

	public void removeValueNames(String valueName) {
		for (int index = 0; index < values.size(); index++) {
			if (values.get(index).getName().equalsIgnoreCase(valueName)) {
				values.remove(index);
			}
		}
		// values.add(new LinguisticValue(valueName));

	}

	public ArrayList<LinguisticValue> getLinguisticValues() {
		return values;
	}

	public void drawGraph(Graphics g) {
		double maxX = 0;
		double minX = Double.MAX_VALUE;
		for (LinguisticValue value : values) {
			if (value.getMaxX() > maxX) {
				maxX = value.getMaxX();
			}
			if (value.getMinX() < minX) {
				minX = value.getMinX();
			}
		}
		maxX += (maxX - minX) * 0.1;
		minX -= (maxX - minX) * 0.1;
		for (int i = 0; i < values.size(); i++) {
			LinguisticValue value = values.get(i);
			Color c = Color.BLACK;
			if (i > 4) {
				Random randomer = new Random();
				c = new Color(randomer.nextInt(255), randomer.nextInt(255),
						randomer.nextInt(255));
			} else {
				c = colors[i];
			}
			value.drawLine(g, minX, maxX, c);
		}
	}

}
